package com.broadengate.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* 设定Request的字符集，该Filter通过web.xml加载生效
* 
* @author jin
*/

public class EncodingFilter implements Filter {
	/* 默认的字符编码格式utf-8 */
	private static final String DEFAULT_ENCODEING = "utf-8";

	/* 声明字符编码格式变量 */
	private String encoding = null;

	/**
	 * 初始化方�?
	 * 
	 * @param argFilterConfig 过滤配置信息
	 * @exception ServletException Servlet 异常
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig argFilterConfig) throws ServletException {
		encoding = argFilterConfig.getInitParameter("encoding");
		if (encoding == null || "".equals(encoding)) {
			encoding = DEFAULT_ENCODEING;
		}
	}

	/**
	 * �?毁处理方�?
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * HTTP 请求字符编码过滤处理
	 * 
	 * @param argRequest ServletRequest
	 * @param argResponse ServletResponse
	 * @param argChain FilterChain
	 * @exception IOException 数据流异�?
	 * @exception ServletException servlet异常
	 *
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(
		ServletRequest argRequest,
		ServletResponse argResponse,
		FilterChain argChain)
		throws IOException, ServletException {
		//编码设定处理
		argRequest.setCharacterEncoding(encoding);
		argChain.doFilter(argRequest, argResponse);
	}
}
